<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="98px" ref="queryForm" size="small" v-show="showSearch">
      <el-form-item label="框架编号" prop="projectCode">
        <el-input @keyup.enter.native="handleQuery" clearable style="width: 240px;" placeholder="请输入框架编号" size="mini"
          v-model="queryParams.projectCode" />
      </el-form-item>
      <el-form-item label="订单编号" prop="code">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="请输入订单编号" v-model="queryParams.code"
          size="mini" />
      </el-form-item>
      <el-form-item label="业务员" prop="salesmanName">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="请输入业务员" size="mini"
          v-model="queryParams.salesmanName" />
        <!--<el-select v-model="queryParams.salesmanId" placeholder="请选择业务员" filterable clearable style="width: 100%"  size="mini">
          <el-option v-for="item in salesmanList" :key="item.userId" :label="item.nickName" :value="item.userId"/>
        </el-select>-->
      </el-form-item>
      <el-form-item label="买方名称" prop="customName">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="请输入买方名称" size="mini"
          v-model="queryParams.customName" />
      </el-form-item>

      <el-form-item label="制单人" prop="createName">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="请输入制单人" size="mini"
          v-model="queryParams.createName" />
      </el-form-item>
      <!--<el-form-item label="下单日期">
        <el-date-picker v-model="dateRange" style="display: flex;" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>-->
      <el-form-item label="项目状态" prop="status">
        <el-select clearable placeholder="请选择项目状态" style="width: 200px" v-model="queryParams.status" size="mini">
          <el-option :key="dict.value" :label="dict.label" :value="dict.value" v-for="dict in dict.type.pro_status" />
        </el-select>
      </el-form-item>

      <el-form-item label="订单类别" prop="goodsType">
        <el-select clearable placeholder="选择订单类别" style="width: 200px" v-model="queryParams.goodsType" size="mini">
          <el-option :key="dict.value" :label="dict.label" :value="dict.value"
            v-for="dict in dict.type.order_goods_type" />
        </el-select>
      </el-form-item>

      <el-form-item label="合同交货期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table border :data="dataList" @sort-change="handleSortChange" :max-height="tableMaxHeight" style="width: 100%"
      @selection-change="handleSelectionChange" v-loading="loading" show-summary :summary-method="getSummaries">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="框架编号" prop="projectCode" />
      <el-table-column sortable align="center" label="订单编号" prop="code">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="handleUpdateDetail(scope.row)" style="color: #00afff;cursor:pointer;">
              {{ scope.row.code }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="合同交货期" prop="deliveryTime" />
      <el-table-column sortable align="center" label="下单时间" prop="orderDate" />
      <el-table-column sortable align="center" label="买方" prop="customName" />
      <el-table-column sortable align="center" label="业务员" prop="salesmanName" />
      <el-table-column sortable align="center" label="制单人" prop="createName" />
      <el-table-column sortable align="center" label="项目状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="地区" prop="area">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_area" :value="scope.row.area" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="来源" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="订单类型" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_goods_type" :value="scope.row.goodsType" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="订单金额" prop="contractMoney" />
      <el-table-column align="center" label="开票金额" prop="taxAll" />
      <el-table-column align="center" label="剩余开票金额" prop="taxRemain" />
      <el-table-column align="center" label="回款金额" prop="backMoney" />
      <el-table-column align="center" label="剩余回款金额" prop="remainMoney">
        <template slot-scope="scope">
          <span v-if="scope.row.remainMoney > 0" style="color:red;font-weight: bold">{{ scope.row.remainMoney }}</span>
          <span v-if="scope.row.remainMoney <= 0" style="color:green;font-weight: bold">{{ scope.row.remainMoney
            }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="备注信息" align="center" prop="information" show-overflow-tooltip/>
      <el-table-column align="center" label="附件" prop="attachment" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.attachment"></file-upload-show>
        </template>
      </el-table-column>-->
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <project-base-info-form-for-prj @ok="getList" ref="ProjectBaseInfoFormForPrj"></project-base-info-form-for-prj>
  </div>
</template>

<script>
import { accAdd, deleteAction, downloadFile, getAction } from '@/api/manage'
import ProjectBaseInfoFormForPrj from "@/views/develop/projectBaseInfo/module/ProjectBaseInfoFormForPrj";
export default {
  name: "ProjectZdList",
  dicts: ['pro_status', 'order_area', 'order_goods_type', "project_source"],
  components: {
    ProjectBaseInfoFormForPrj,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单基础信息管理表格数据
      dataList: [],
      salesmanList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        yearStr: null,
        pageNum: 1,
        pageSize: 20,
        name: null,
        status: null,
        code: null,
        master: null,
        customId: null,
        projectCode: null,
        salesmanName: null,
        createName: null,
        customName: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        goodsType: null,
        auditMoney: null,
        checkDate: null,
        checkState: null,
        fundCallback: null,
        information: null,
        source: null,
        orderDate: null,
        //orderByColumn: "create_time",
        orderByColumn: "remainMoney",
        isAsc: "desc",
        params: {},
      },
      url: {
        list: '/projectBaseInfo/projectBaseInfo/list',
      },
      tableMaxHeight: window.innerHeight - 320,
      // 部门树选项
      deptOptions: [],
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.initQuery();
    this.getList();
  },
  methods: {
    handleExport() {
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出项目销售信息表?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        downloadFile('/projectBaseInfo/projectBaseInfo/exportPro', that.queryParams);
      });
    },
    handleUpdateDetail(row) {
      this.$refs.ProjectBaseInfoFormForPrj.handleUpdateDetail(row);
    },    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    initQuery() {
      let that = this;
      if (that.$route.query.dataFlag) {
        that.queryParams.params.dataFlag = that.$route.query.dataFlag;
      }
    },
    /** 查询项目基础信息管理列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange);
      //.addDateRange(this.queryParams, this.dateRange);
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "contractMoney" || column.property === "taxAll" || column.property === "taxRemain"
          || column.property === "backMoney" || column.property === "remainMoney") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },

  }
};
</script>
